$(document).ready(function() {
  var webHost = 'http://' + window.location.host;

  var stateObject = {id: 1};
  var title = "2017 OPPO移动开发者大会";
  var newUrl = "";
  // history.pushState(stateObject,title,newUrl);
  var ajax_obj = {};

  function get_url_parameter() {
    //var url_search = window.location.search;
    //获取?之后，#之前的值
    var url_search = window.location.hash;
    //获取#之后的值
    ajax_obj.this_page = '';
    ajax_obj.this_type = '';
    var searchArray = url_search.substring(1).split("&");
    for (var i = 0; i < searchArray.length; i++) {
      var temp = searchArray[i].split('=');
      if (temp[0] == 'page') {
        //判断浏览器 URL 地址是否找到参数为 client_id 的
        //alert(temp[1]);
        //得到 参数为 client_id 的值
        ajax_obj.this_page = temp[1];
        sessionStorage.setItem("page", temp[1]);
      }
      if (temp[0] == 'type') {
        //判断浏览器 URL 地址是否找到参数为 client_id 的
        //alert(temp[1]);
        //得到 参数为 client_id 的值
        ajax_obj.this_type = temp[1];
        sessionStorage.setItem("page", temp[1]);
      }
    }
    go_ajax();
  }

  function go_ajax(){
    if ($.cookie('OPPO_ADMIN_uname') != undefined && $.cookie('OPPO_ADMIN_uname') != 'null') {
      //已登录
      if(ajax_obj.this_page!=''&&ajax_obj.this_type!=''){
        get_lists_obj.page = ajax_obj.this_page;
        if(ajax_obj.this_type==1){
          get_lists();
        }else{
          get_lists2();
        }
      }else{
        get_lists();
      }
    } else {
      $('.homePage').show();
      $('.body_main').hide();
      get_lists_obj.page = 1;
    }
  }

  /*分页*/
  function page(pageDiv, curPage, total, pageSize, totalPage) {
    // var curPage = 1; //当前页码
    // var total, pageSize, totalPage; //总记录数，每页显示数，总页数

    //获取分页条

    function getPageBar(pageDiv, curPage, total, pageSize, totalPage) {
      $('#' + pageDiv + '').find('*').remove();
      //页码大于最大页数
      if (curPage > totalPage) curPage = totalPage;
      //页码小于1
      if (curPage < 1) curPage = 1;
      //pageStr = "<span>" + curPage + "/" + totalPage + "</span><span>共" + total + "条</span>";
      var pageStr = '';

      //如果是第一页
      if (curPage == 1) {
        pageStr += "<span>首页</span><span>上一页</span>";
      } else {
        pageStr += "<span class='btnSpan'><a href='javascript:void(0)' rel='1'>首页</a></span><span class='btnSpan'><a href='javascript:void(0)' rel='" + (curPage - 1) + "'>上一页</a></span>";
      }

      if (totalPage >= 1) {
        if (curPage - 2 >= 1) {
          //前两页
          for (var i = 2; i >= 1; i--) {
            pageStr += "<span class='btnSpan noBorder'><a href='javascript:void(0)' rel='" + (parseInt(curPage) - i) + "'>" + (parseInt(curPage) - i) + "</a></span>";
            //上2个页号
          }
        } else if (curPage - 1 >= 1) {
          //前一页
          pageStr += "<span class='btnSpan noBorder'><a href='javascript:void(0)' rel='" + (parseInt(curPage) - 1) + "'>" + (parseInt(curPage) - 1) + "</a></span>";
        }
        pageStr += "<span class='btnSpan thisPage'><a href='javascript:void(0)' rel='" + (parseInt(curPage)) + "'>" + curPage + "</a></span>";
        //当前页面
        if (parseInt(curPage) + 2 <= parseInt(totalPage)) {
          //前两页
          for (var i = 1; i <= 2; i++) {
            pageStr += "<span class='btnSpan noBorder'><a href='javascript:void(0)' rel='" + (parseInt(curPage) + i) + "'>" + (parseInt(curPage) + i) + "</a></span>";
            //上2个页号
          }
        } else if (parseInt(curPage) + 1 <= parseInt(totalPage)) {
          //前一页
          pageStr += "<span class='btnSpan noBorder'><a href='javascript:void(0)' rel='" + (parseInt(curPage) + 1) + "'>" + (parseInt(curPage) + 1) + "</a></span>";
        }
      }

      //如果是最后页
      if (curPage >= totalPage) {
        pageStr += "<span>下一页</span><span>尾页</span>";
      } else {
        pageStr += "<span class='btnSpan'><a href='javascript:void(0)' rel='" + (parseInt(curPage) + 1) + "'>下一页</a></span><span class='btnSpan'><a href='javascript:void(0)' rel='" + totalPage + "'>尾页</a></span>";
      }
      pageStr += "<span class='text'>" + curPage + "/" + totalPage + "页</span><span class='text'>共" + total + "条</span>";
      $('#' + pageDiv + '').append(pageStr);
    }
    getPageBar(pageDiv, curPage, total, pageSize, totalPage);
  }
  /*分页 结束*/

  /*验证手机号格式，是否正确*/
  var onlyPhone = /^(((11[0-9]{1}|)|(12[0-9]{1}|)|(13[0-9]{1})|(14[0-9]{1}|)|(15[0-9]{1})|(16[0-9]{1}|)|(17[0-9]{1}|)|(18[0-9]{1})|(19[0-9]{1}|)|(10[0-9]{1}|))+\d{8})$/;
  /*验证手机号格式，是否正确*/

  var user_login_obj = {};
  user_login_obj.user = '';
  user_login_obj.password = '';

  function user_login() {

    $('.homePage .loginBtn').click(function() {
      user_login_obj.user = $('.homePage .user').val();
      user_login_obj.password = $('.homePage .password').val();
      if (user_login_obj.user != '' && user_login_obj.password != '') {
        user_login_ajax();
      } else {
        alert('请将信息填写完整');
      }
    });
  }
  user_login();

  function user_login_ajax() {
    $.ajax({
      url: '' + webHost + '/admin/login',
      type: 'POST',
      data: {
        username: user_login_obj.user,
        password: user_login_obj.password,
      },
      success: function(json) {
        if (json.code == 200) {
          var newUrl = "./admin2.html";
          history.pushState(stateObject,title,newUrl);
          $('.homePage').hide();
          $('.body_main').show();
          get_lists();
        } else {
          alert(json.msg);
        }
      },
      error: function(json) {
        alert(json.msg);
      }
    });
  }

  var check_num = 1;
  /*获取列表*/
  var get_lists_obj = {};

  $('.show_add').click(function() {
    var newUrl = "./admin2.html#page=1&type=1";
    history.pushState(stateObject,title,newUrl);
    get_lists_obj.page = 1;
    get_lists();
  });

  function get_lists() {
    $('.add_invitation').hide();
    $('.show_add').addClass('btnInfo').removeClass('btnDefault');
    $('.show_invitation').addClass('btnDefault').removeClass('btnInfo');
    check_num = 1;
    $('.homePage').hide();
    $('.body_main').show();
    $.ajax({
      url: '' + webHost + '/admin/lists',
      type: 'POST',
      data: {
        page: get_lists_obj.page,
        sign_type: 1,
      },
      success: function(json) {
        if (json.code == 200) {
          build_lists_table(json);
          $('.error_table').hide();
          $('.table_box,#page').show();
        } else {
          $('.table_box,#page').hide();
          $('.error_table').text(json.msg).show();
        }
      },
      error: function(json) {
        $('.table_box,#page').hide();
        $('.error_table').text(json.msg).show();
      }
    });
  }

  function build_lists_table(json) {
    total = json.data.count; //总记录数
    pageSize = 10; //每页显示条数
    curPage = json.data.page; //当前页
    totalPage = json.data.totalPages; //总页数
    page('page', curPage, total, pageSize, totalPage);
    //page在public.js里，'page'为分页div的id名
    var string = '';
    var status_text = '';
    var shenpi_btn = '';
    $.each(json.data.list, function(index, array) { //遍历json数据列

      switch (array.status) {
        case '0':
          status_text = '审核中';
          shenpi_btn = '<td class="shenpi" data-name="' + array.name + '" data-mobile="' + array.mobile + '">操作</td>';
          break;
        case '1':
          status_text = '已审核';
          shenpi_btn = '<td class="wu_shenpi">操作</td>';
          break;
        case '2':
          status_text = '已拒绝';
          shenpi_btn = '<td class="wu_shenpi">操作</td>';
          break;
        default:
      }
      $('.table_box thead').html('<th>序列</th><th>报名时间</th><th>姓名</th><th>手机号</th><th>公司名称</th><th>产品名称</th><th>报名专场</th><th>审核状态</th><th>操作</th>');
      string += '<tr><td>' + array.id + '</td><td>' + array.add_time + '</td><td>' + array.name + '</td><td>' + array.mobile + '</td><td>' + array.company + '</td><td>' + array.products + '</td><td>' + array.type + '</td><td>' + status_text + '</td>' + shenpi_btn + '</tr>';
    });
    $('.table_box tbody').html(string);
  }
  /*获取列表 结束*/

  /*制作媒体合同表格页码点击*/
  function mediaContract_table_page() {
    $('#page').on('click', 'span a', function() {
      var rel = $(this).attr("rel");
      if (rel) {
        get_lists_obj.page = rel;
        if (check_num == 1) {
          var newUrl = "./admin2.html#page="+get_lists_obj.page+"&type=1";
          history.pushState(stateObject,title,newUrl);
          get_lists();
        } else {
          var newUrl = "./admin2.html#page="+get_lists_obj.page+"&type=2";
          history.pushState(stateObject,title,newUrl);
          get_lists2();
        }
      }
    });
  }
  mediaContract_table_page();
  /*制作媒体合同表格页码点击 结束*/

  var examine_status_obj = {};
  $('body').on('click', '.body_main .table .shenpi', function() {
    examine_status_obj.name = $(this).attr('data-name');
    examine_status_obj.mobile = $(this).attr('data-mobile');
    $('.shenpiModal span.name').text(examine_status_obj.name);
    $('.shenpiModal').modal('show');
  });

  $('.shenpiModal .noShenpi').click(function() {
    examine_status_obj.status = 2;
    examine_status_ajax();
  });

  $('.shenpiModal .submitShenpi').click(function() {
    examine_status_obj.status = 1;
    examine_status_ajax();
  });

  function examine_status_ajax() {
    $.ajax({
      url: '' + webHost + '/admin/examine_status',
      type: 'POST',
      data: {
        mobile: examine_status_obj.mobile,
        status: examine_status_obj.status
      },
      success: function(json) {
        if (json.code == 200) {
          //send_sms_ajax();
          alert(json.msg);
        } else {
          $('.errorShenpi').text(json.msg).show();
        }
      },
      error: function(json) {
        $('.errorShenpi').text(json.msg).show();
      }
    });
  }

  function send_sms_ajax() {
    $.ajax({
      url: '' + webHost + '/admin/send_sms',
      type: 'POST',
      data: {
        mobile: examine_status_obj.mobile,
        send_type: examine_status_obj.status
      },
      success: function(json) {
        if (json.code == 200) {
          $('.errorShenpi').hide();
          //alert(json.msg);
          $('.shenpiModal').modal('hide');
        } else {
          $('.errorShenpi').text(json.msg).show();
        }
      },
      error: function(json) {
        $('.errorShenpi').text(json.msg).show();
      }
    });
  }

  var add_invitation_obj = {};
  $('.add_invitation').click(function() {
    $('.add_yaoqingModal').modal('show');
  })

  $('.submitYaoqing').click(function() {
    add_invitation_obj.mobile = $('.add_yaoqingModal .mobile').val();
    add_invitation_obj.name = $('.add_yaoqingModal .name').val();
    add_invitation_obj.company = $('.add_yaoqingModal .company').val();
    add_invitation_obj.products = $('.add_yaoqingModal .products').val();
    add_invitation_obj.type = $('.add_yaoqingModal .type').children('option:selected').val();
    if (add_invitation_obj.mobile != '' && add_invitation_obj.name != '' && add_invitation_obj.company != '' && add_invitation_obj.products != '' && add_invitation_obj.type != '') {
      $('.errorYaoqing').hide();
      add_invitation_ajax();
    } else {
      $('.errorYaoqing').text('请将信息填写完整').show();
    }
  });

  function add_invitation_ajax() {
    $.ajax({
      url: '' + webHost + '/admin/add',
      type: 'POST',
      data: {
        mobile: add_invitation_obj.mobile,
        name: add_invitation_obj.name,
        company: add_invitation_obj.company,
        products: add_invitation_obj.products,
        type: add_invitation_obj.type
      },
      success: function(json) {
        if (json.code == 200) {
          $('.errorYaoqing').hide();
          //alert(json.msg);
          $('.add_yaoqingModal').modal('hide');
          $('.add_yaoqingModal .mobile').val('');
          $('.add_yaoqingModal .name').val('');
          $('.add_yaoqingModal .company').val('');
          $('.add_yaoqingModal .products').val('');
          $('.add_yaoqingModal .type').val('');
          get_lists_obj.page = 1;
          get_lists2();
        } else {
          $('.errorYaoqing').text(json.msg).show();
        }
      },
      error: function(json) {
        $('.errorYaoqing').text(json.msg).show();
      }
    });
  }

  $('.show_invitation').click(function() {
    var newUrl = "./admin2.html#page=1&type=2";
    history.pushState(stateObject,title,newUrl);
    get_lists_obj.page = 1;
    get_lists2();
  })

  function get_lists2() {
    $('.add_invitation').show();
    check_num = 2;
    $('.show_add').addClass('btnDefault').removeClass('btnInfo');
    $('.show_invitation').addClass('btnInfo').removeClass('btnDefault');
    $('.homePage').hide();
    $('.body_main').show();
    $.ajax({
      url: '' + webHost + '/admin/lists',
      type: 'POST',
      data: {
        page: get_lists_obj.page,
        sign_type: 2,
      },
      success: function(json) {
        if (json.code == 200) {
          build_lists_table2(json);
          $('.error_table').hide();
          $('.table_box,#page').show();
        } else {
          $('.table_box,#page').hide();
          $('.error_table').text(json.msg).show();
        }
      },
      error: function(json) {
        $('.table_box,#page').hide();
        $('.error_table').text(json.msg).show();
      }
    });
  }

  function build_lists_table2(json) {
    total = json.data.count; //总记录数
    pageSize = 10; //每页显示条数
    curPage = json.data.page; //当前页
    totalPage = json.data.totalPages; //总页数
    page('page', curPage, total, pageSize, totalPage);
    //page在public.js里，'page'为分页div的id名
    var string = '';
    var status_text = '';
    $.each(json.data.list, function(index, array) { //遍历json数据列

      switch (array.status) {
        case '0':
          status_text = '审核中';
          break;
        case '1':
          status_text = '已邀请';
          break;
        case '2':
          status_text = '已拒绝';
          break;
        default:
      }

      string += '<tr><td>' + array.id + '</td><td>' + array.add_time + '</td><td>' + array.name + '</td><td>' + array.mobile + '</td><td>' + array.company + '</td><td>' + array.products + '</td><td>' + array.type + '</td><td>' + status_text + '</td></tr>';
    });
    $('.table_box thead').html('<th>序列</th><th>邀请时间</th><th>姓名</th><th>手机号</th><th>公司名称</th><th>产品名称</th><th>邀请专场</th><th>审核状态</th>');
    $('.table_box tbody').html(string);
  }

  get_url_parameter();
  //获取url的参数值

})